package cn.rkylin.oms.system.shop.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.common.jobs.AbstractJob;
import cn.rkylin.oms.system.shop.dao.IShopDAO;
import cn.rkylin.oms.system.shop.domain.Shop;
import cn.rkylin.oms.system.shop.vo.ShopVO;

/**
 * 店铺管理服务实现
 * 
 * @author wangxiaoyi
 * @version 1.0
 * @created 13-1月-2017 09:11:15
 */
@Service("shopService")
public class ShopServiceImpl extends ApolloService implements IShopService {
    // 调度服务器的url地址，见app.properties
    @Value("${create.shop.jobs}")
    private String createShopJobs;

    // 调度服务器的url地址，见app.properties
    @Value("${scheduler.addJob.url}")
    private String addJobURL;

    /**
     * 店铺数据访问
     */
    @Autowired
    private IShopDAO shopDAO;

    public IShopDAO getShopDAO() {
        return shopDAO;
    }

    /**
     * 构造函数
     */
    public ShopServiceImpl() {

    }

    /**
     * 查询需要分单店铺
     * 
     * @param shopVO
     */
    public PageInfo<ShopVO> findSplitShop(int page, int rows, ShopVO shopVO) throws Exception {
        PageInfo<ShopVO> shopVOList = findPage(page, rows, "pageSelectSplitShop", shopVO);
        return shopVOList;
    }

    /**
     * 查询店铺
     * 
     * @param shopVO
     */
    public PageInfo<ShopVO> findByWhere(int page, int rows, ShopVO shopVO) throws Exception {
        PageInfo<ShopVO> shopVOList = findPage(page, rows, "pageSelectShop", shopVO);
        return shopVOList;
    }

    /**
     * 创建店铺
     * 
     * @param shopVO
     */
//    @CachePut(value = "shop", key = "T(String).valueOf('shop:').concat(#shopVO.shopId)")
    public ShopVO insert(ShopVO shopVO) throws Exception {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopVO, shop);
        shopDAO.insert(shop);

        return shopVO;
    }
    
    @SuppressWarnings("unchecked")
	public void initShop(String shopid) throws Exception {
		ShopVO shopVO = getShopById(shopid);
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopVO, shop);
        
        // 创建店铺对应的作业（quartz平台）
        // 订单下载作业(定时任务)
        AbstractJob orderDownJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("orderDownloadJob");
        orderDownJob.createJob(shop);

        // 订单更新作业(定时任务)
        AbstractJob orderUpdateJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("orderUpdateJob");
        orderUpdateJob.createJob(shop);

        // 商品下载作业(定时任务)
        //AbstractJob productDownJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("productDownloadJob");
        //productDownJob.createJob(shop);

        // 退款单下载作业(定时任务)
        AbstractJob returnOrderDownJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("returnOrderDownloadJob");
        returnOrderDownJob.createJob(shop);

        // 店铺api token刷新作业(定时任务)
        /*AbstractJob shopTokenRefreshJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("shopTokenRefreshJob");
        shopTokenRefreshJob.createJob(shop);*/

        // 对帐单下载任务(定时任务)
        //AbstractJob accountReportDownloadJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("accountReportDownloadJob");
        //accountReportDownloadJob.createJob(shop);

        // 系统订单生成任务(定时任务)
        AbstractJob sysOrderGeneration = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("sysOrderGeneration");
        sysOrderGeneration.createJob(shop);

        // 系统订单审核分派(定时任务)
        AbstractJob sysOrderVerifyAssign = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("sysOrderVerifyAssign");
        sysOrderVerifyAssign.createJob(shop);
        
        // 平台发货
        AbstractJob platformConsign = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("platformConsign");
        platformConsign.createJob(shop);
        
        // 自动同步库存
        //AbstractJob stockSynchronization = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("stockSynchronization");
        //stockSynchronization.createJob(shop);
        
        // 系统退款单生成
        AbstractJob sysRefundGeneration = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("sysRefundGeneration");
        sysRefundGeneration.createJob(shop);
        
        // 系统退款单更新
        AbstractJob sysRefundUpdate = (AbstractJob) ContextLoader.getCurrentWebApplicationContext().getBean("sysRefundUpdate");
        sysRefundUpdate.createJob(shop);
    }

    /**
     * 修改店铺，
     * 
     * 同时，清除缓存中的数据，而非更新缓存中的数据。 防止在局部更新对象数据时，缓存对象的其它数据被还原成默认值。
     * 
     * @param shopVO
     */
    @Transactional
//    @CacheEvict(value = "shop", key = "T(String).valueOf('shop:').concat(#shopVO.shopId)")
    public ShopVO update(ShopVO shopVO) throws Exception {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopVO, shop);
        shopDAO.update(shop);
        return shopVO;
    }

    /**
     * 删除店铺
     * 
     * @param shopVO
     */
    @Transactional
//    @CacheEvict(value = "shop", key = "T(String).valueOf('shop:').concat(#shopVO.shopId)")
    public int delete(ShopVO shopVO) throws Exception {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopVO, shop);
        return shopDAO.delete(shop);
    }

    /**
     * 根据shopId获取shop
     * 
     * @param shopId
     * @return
     */
//    @Cacheable(value = "shop", key = "T(String).valueOf('shop:').concat(#shopId)", unless = "#result==null")
    public ShopVO getShopById(String shopId) throws Exception {
        ShopVO returnShop = shopDAO.findById(shopId);
        return returnShop;
    }

    /**
     * 验证店铺
     * 
     * @param shopVO
     */
    public int validate(ShopVO shopVO) throws Exception {
        return 0;
    }

    /**
     * 启用店铺
     * 
     * @param shopId
     */
    @Transactional
//    @CacheEvict(value = "shop", key = "T(String).valueOf('shop:').concat(#shopId)")
    public int setEnable(String shopId) throws Exception {
        Shop shop = new Shop();
        shop.setShopId(shopId);
        return shopDAO.update(shop);
    }

    /**
     * 禁用店铺
     * 
     * @param shopId
     */
    @Transactional
//    @CacheEvict(value = "shop", key = "T(String).valueOf('shop:').concat(#shopId)")
    public int setDisable(String shopId) throws Exception {
        Shop shop = new Shop();
        shop.setShopId(shopId);
        return shopDAO.update(shop);
    }

    /**
     * (non-Javadoc) 获取店铺列表，前100个店铺
     */
    @Override
    public List<ShopVO> findByWhere(ShopVO shopVO) throws Exception {
        PageInfo<ShopVO> shopVOList = findPage(1, Integer.MAX_VALUE, "pageSelectSplitShop", shopVO);
        return shopVOList.getList();
    }

    /**
     * (non-Javadoc) 获取店铺列表
     */
    @Override
    public List<ShopVO> findByWhereAll(ShopVO shopVO) throws Exception {
        return shopDAO.findByWhere(shopVO);
    }
    
    @Override
    public List<ShopVO> findByWhereByShopAccount(ShopVO shopVO) throws Exception {
    	return shopDAO.findByWhereByShopAccount(shopVO);
    }
    
}